package logic

import (
	"box/boxrpc/boxrpc"
	"box/boxrpc/internal/svc"
	"box/models"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopAddLogic {
	return &ShopAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *ShopAddLogic) ShopAdd(in *boxrpc.ShopAddRequest) (*boxrpc.ShopAddResponse, error) {
	// todo: add your logic here and delete this line

	var shop models.Shop
	models.DB.Debug().Where("name = ?", in.Name).Find(&shop)
	if shop.ID != 0 {
		return nil, fmt.Errorf("该商品已存在添加失败")
	}

	shop = models.Shop{
		Name:    in.Name,
		Profile: in.Profile,
		Weight:  float64(in.Weight),
		Price:   float64(in.Price),
	}

	models.DB.Debug().Create(&shop)

	return &boxrpc.ShopAddResponse{
		ShopId: int64(shop.ID),
	}, nil
}
